//
//  AppDelegate.swift
//  STVVeibo
//
//  Created by leergou on 16/7/19.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
//        STSQLManager.sharedTools
        
        // 设置 APP 颜色
        setupAppStyle()
        // 初始化window
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // 设置背景颜色
        self.window?.backgroundColor = UIColor.whiteColor()
        // 设置跟控制器
        setRootViewController()
        
//        self.window?.rootViewController = UINavigationController(rootViewController: STComposeController())
        // 设置成为主窗口,并可见
        self.window?.makeKeyAndVisible()
        // 接收通知,切换跟控制器
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootViewController:", name: SwitchRootViewContrller, object: nil)
        return true
    }
    
    // MARK: 程序启动设置默认跟控制器
    func setRootViewController(){
        
        // 如果用户已经登录 rootVC = WELCOME
        // 没有登录 rootVC= mianvc
        if STUserAccountViewModel.sharedUserAccountTools.isLogin {
            // 已经登录,判断版本
            if isNewVersion(){
                // 是新版本
                self.window?.rootViewController = STNewFeatureController()
            } else {
                // 不是新版本
                self.window?.rootViewController = STMainViewController()
            }
        } else {
            // 没有登录
            self.window?.rootViewController = STMainViewController()
        }
    }
    
    
    // MAKR: 接受到通知切换跟控制器
    func switchRootViewController(notice:NSNotification){
        
        if notice.object == nil {
            self.window?.rootViewController = STWelcomeController()
        } else {
            
            self.window?.rootViewController = STMainViewController()
        }
    }
    
    
    // 判断app版本
    func isNewVersion() -> Bool{
        // 获取当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        // 把字符串转成 double
        let currentVersionD = Double(currentVersion)!
        
        // 获取偏好保存的版本
        let stAppVersion = "stAppVersion"
        let savedVersion = NSUserDefaults.standardUserDefaults().doubleForKey(stAppVersion)
        
        // 保存当前版本-每次都更新
        NSUserDefaults.standardUserDefaults().setDouble(currentVersionD, forKey: stAppVersion)
        // 返回是否是新版本
        return currentVersionD > savedVersion
    }
    

    func setupAppStyle() {
        
        // 设置 tintColor(这里设置全局生效)
//        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = ThemeColor
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    func applicationDidEnterBackground(application: UIApplication) {
        STStatusDAL.deleteCach()
    }
    
    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }



}

